var updateProfileURL = "/profile/updateProfile"
$(function () {
    var dataContent = new Vue({
        el: '#profileForm',
        data: {
            profile: {}
        },
        methods: {},
        created: function () {
            var _this = this;
            $.getJSON("/profile/initData", {}, function (resp) {
                if (resp.code != "0000") {
                    showMessage(resp.code || resp.desc, "error");
                } else {
                    _this.profile = resp.data;
                }
            })
        }
    });

    $("#btn-profile-edit").click(function (e) {
        $("#btn-profile-edit").hide();
        $("#btn-profile-save,#btn-profile-reset").removeClass("hidden");
        $("#profileForm .form-control").removeClass("hidden");
        $("#profileForm .form-control-static").addClass("hidden");
    });
    $("#btn-profile-reset").click(function (e) {
        $("#btn-profile-edit").show();
        $("#btn-profile-save,#btn-profile-reset").addClass("hidden");
        $("#profileForm .form-control").addClass("hidden");
        $("#profileForm .form-control-static").removeClass("hidden");
    });
    var updateValidation = $("#profileForm").validate({
        rules: {
            nickName: {required: true, maxlength: 100},
            gender: {required: true, maxlength: 2},
            mobile: {required: true, maxlength: 20},
            email: {required: true, maxlength: 100, email: true}
        }
    });
    $("#profileForm").ajaxForm({
        target: '#btn-profile-save',   // target element(s) to be updated with server response
        dataType: 'json',
        url: updateProfileURL,
        beforeSubmit: function (formData, jqForm, options) {
            return updateValidation.valid();
        },
        success: function (result, statusText, xhr, $form) {
            if (result.code != "0000") {
                showMessage(result.code || result.desc, "error");
            } else {
                $("#btn-profile-edit").show();
                $("#btn-profile-save,#btn-profile-reset").addClass("hidden");
                $("#profileForm .form-control").addClass("hidden");
                $("#profileForm .form-control-static").removeClass("hidden");
            }
        }
    });
})